package com.example.icedcap.topeka_fake.activity;

import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toolbar;

import com.example.icedcap.topeka_fake.R;
import com.example.icedcap.topeka_fake.fragment.CategorySelectorFragment;
import com.example.icedcap.topeka_fake.model.Player;
import com.example.icedcap.topeka_fake.widget.AvatarView;

/**
 * Author: doushuqi
 * Date: 15/7/13
 * Time: 上午10:23
 * Email: shuqi.dou@singuloid.com
 */
public class CategorySelectionActivity extends Activity {
    private static final String TAG = CategorySelectionActivity.class.getSimpleName();
    private static final String EXTRA_PLAYER = "player";

    public static void start(Context context, Player player) {
        Intent starter = new Intent(context, CategorySelectionActivity.class);
        starter.putExtra(EXTRA_PLAYER, player);
        context.startActivity(starter);
    }

    public static void start(Context context, Player player, ActivityOptions options) {
        Intent starter = new Intent(context, CategorySelectionActivity.class);
        starter.putExtra(EXTRA_PLAYER, player);
        context.startActivity(starter, options.toBundle());
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_selector);
        Player player = getIntent().getParcelableExtra(EXTRA_PLAYER);
        setUpToolbar(player);
        if (null == savedInstanceState) {
            attachCategoryFragment();
        } else {
            setProgressBarVisibility(View.GONE);
        }
    }

    private void attachCategoryFragment() {
        getFragmentManager().beginTransaction().replace(R.id.quiz_container,
                CategorySelectorFragment.newInstance()).commit();
        setProgressBarVisibility(View.GONE);
    }

    private void setProgressBarVisibility(int visibility) {
        findViewById(R.id.progress).setVisibility(visibility);
    }

    private void setUpToolbar(Player player) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_player);
        setActionBar(toolbar);
        getActionBar().setDisplayShowTitleEnabled(false);
        final AvatarView avatarView = (AvatarView) findViewById(R.id.avatar);
        avatarView.setImageResource(player.getAvatar().getDrawableResId());
        ((TextView)findViewById(R.id.title)).setText(getDisplayName(player));
    }

    private String getDisplayName(Player player) {
        return getString(R.string.player_display_name, player.getFirstName(), player.getLastInitial());
    }
}
